<?php
// +----------------------------------------------------------------------
// | Description: 解决跨域问题
// +----------------------------------------------------------------------
// | Author: 董鹏
// +----------------------------------------------------------------------

namespace app\common\controller;

use think\Controller;
use think\Request;
use think\Response;
use think\exception\HttpResponseException;

class Common extends Controller
{
    public $param;
    public $MylogData;
    public function _initialize()
    {
        parent::_initialize();
        /*防止跨域*/
        header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");
        $param = Request::instance()->param();
        $this->param = $param;
    }

    public function object_array($array)
    {
        if (is_object($array)) {
            $array = (array) $array;
        }
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }

    /**
     * 返回json格式数据 作用为检查程序是否终止 并抛出异常
     * @param bool $res 某代码段运行的结果 
     * @param int $error 错误消息  
     * @param mixed $data 需要返回的数据 
     * @param int $code 错误码
     * @param int $type 具体错误类型
     * @return string json格式的字符串
     */
    public function checkRes($res, $data, $error = '类型异常',$code = null, $type = 1, $dataType = 'json', array $header = [])
    {
        if($res){
            $code = $code ? $code : 200;
            $result = [
                'code'  => $code,
                'data'  => $data
            ];
        }else{
            $code = $code ? $code : 400;
            $result = [
                'code'   => $code,
                'type'   => $type,
                'error'  => $error
            ];
        }
        $response = Response::create($result, $dataType)->header($header);
        throw new HttpResponseException($response);
    }

    /**
     * 返回json格式数据 并且标注状态码为成功
     * @param $error string 错误信息
     * @param $code int 错误码
     * @param $type int 具体错误类型
     * @return string json格式的字符串
     */
    public function abort($res, $error = "操作失败!", $code = 400, $type = 1, $dataType = "json", array $header = [])
    {
        if(!$res){
            $result = [
                'code'  => $code,
                'type'  => $type,
                'error' => $error
            ];
            $response = Response::create($result, $dataType)->header($header);
            throw new HttpResponseException($response);
        }
    }

    /**
     * 返回封装后的API数据到客户端
     * @access protected
     * @param mixed     $data 要返回的数据
     * @param integer   $code 返回的code
     * @param mixed     $error 提示信息
     * @param int       $type 返回数据具体错误类型
     * @param array     $header 发送的Header信息
     * @return void
     */
    protected function jsonData($data, $error = '数据错误!', $type = 1,array $header = [])
    {
        if (is_bool($data) or $data == null) {
            
            $result = [
                'code'  => 400,
                'type'  => $type,
                'error' => $error
            ];
            $response = Response::create($result, 'json')->header($header);
            throw new HttpResponseException($response);
        }
        if (is_object($data)) {
            if (property_exists($data, 'list') and count($data['list']) == 0) {
                $result = [
                    'code'  => 400,
                    'type'  => $type,
                    'error' => $error
                ];
               
                $response = Response::create($result, 'json')->header($header);
                throw new HttpResponseException($response);
            }
        }
        $result = [
            'code'  => 200,
            'data'  => $data
        ];
        $response = Response::create($result, 'json')->header($header);
        throw new HttpResponseException($response);
    }
}
